var a = getApp(), e = require("../../utils/util.js"), t = require("../../nitro.js"), o = require("../../configs.js"), n = e.config("categories"), i = function(a, t) {
    return a.forEach(function(a) {
        a.packages = [], a.gallery = [], t.forEach(function(t) {
            t.selected = !1, t.gallery = e.shuffle(t.gallery).slice(0, 25), t.optionsObject = {}, 
            t.options.forEach(function(a) {
                t.optionsObject[a.title] = a.content;
            }), t.category && t.category.id == a.id && (a.packages.push(t), a.gallery = e.shuffle(a.gallery.concat(t.gallery)).slice(0, 25));
        }), a.hasSubItems = a.title.indexOf("小主题") > -1, a.title.includes("小主题") ? (a.mp_info = n.theme, 
        a.is_themes = !0) : a.title.toLowerCase().includes("look") ? a.mp_info = n.look : a.title.toLowerCase().includes("face") ? a.mp_info = n.face : a.title.toLowerCase().includes("baby") && (a.mp_info = n.baby);
    }), a.filter(function(a) {
        return a.publish;
    });
};

Page({
    data: {
        isIpx: a.globalData.isIpx,
        storeId: "",
        disabledScroll: !1,
        categories: [],
        gallery: {},
        vipBanner: "https://nitro-prod.oss-cn-hangzhou.aliyuncs.com/public/mp-vip-banner.png",
        grouponBanner: "https://nitro-prod.oss-cn-hangzhou.aliyuncs.com/public/mp-nico-6th-banner.jpg",
        grouponTimeleft: "33 小时 22 分 11 秒",
        groupon: {
            title: "85折拼团价",
            price: 2380
        },
        imgUrls: [],
        artworkTabs: !1,
        indicatorDots: !1,
        autoplay: !1,
        interval: 5e3,
        duration: 1e3
    },
    onLoad: function(o) {
        a.globalData.ts = +new Date();
        var n = o.store_id || t.getStore()._id, i = o.category_id, r = o.type, c = e.artworkTabs(i, r), s = c.find(function(a) {
            return a.category_id === i;
        });
        a.globalData.cart.storeId = n, console.info("> artworkTabs", c), console.info("> artworkTab", s), 
        console.info("onLoad options", o), wx.showLoading({}), this.setData({
            brandType: r,
            storeId: n,
            categoryId: i,
            artworkTabs: c,
            className: s ? s.className : r || "default"
        }), this.initData(n, r);
    },
    onReady: function() {
        wx.setNavigationBarColor({
            frontColor: "#000000",
            backgroundColor: "#ffffff",
            animation: {
                duration: 400,
                timingFunc: "easeIn"
            }
        });
    },
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {
        return {
            title: "尼可儿童摄影",
            path: "/pages/stores/stores"
        };
    },
    initData: function(n, r) {
        var c = this, s = n || t.getStore()._id;
        t.fetchProducts(s).then(function(n) {
            var s = i(n.categories, n.items), l = c.calcPackageInfo(s, c.data.categoryId);
            console.info("packageInfo", s, l);
            var g = void 0, u = void 0;
            o.themes_for_baby;
            "kids" !== r && "baby" !== r || (g = s.find(function(a) {
                return a.is_themes;
            }).packages.map(function(a) {
                return {
                    title: a.title,
                    id: a._id,
                    cover: a.gallery.length && a.gallery[0].url.raw,
                    category_id: a.category._id
                };
            }), console.info(">> themePackages", g), u = e.chunkArray(g.filter(function(a) {
                return a.cover;
            }), 6), console.info(">> categories", s)), c.setData({
                store: t.getStore(),
                packageLoaded: !0,
                packageId: l.packages && l.packages.length ? l.packages[0].id : "",
                packageInfo: l,
                categories: s,
                themeGroups: u
            }), c.onSelectPackage(null, c.data.categoryId, c.data.packageId), wx.setNavigationBarTitle({
                title: "" + c.data.store.name
            }), wx.hideLoading(), console.info("duration: product-detail", +new Date() - a.globalData.ts);
        });
    },
    calcPackageInfo: function(a, e) {
        var t = a.find(function(a) {
            return a._id === e;
        });
        if (console.info("calcPackageInfo category", t), t) {
            var o = {
                special_explanation: t.special_explanation,
                mp_info: t.mp_info,
                packages: []
            };
            return t.packages.map(function(a) {
                (a.publish && t.title.includes("小主题") && a.title.includes("T") || !t.title.includes("小主题")) && o.packages.push({
                    title: a.title,
                    id: a._id,
                    options: a.options,
                    price: a.price,
                    description: a.description.replace(/\\r\\n/g, "\n")
                });
            }), o.packages.sort(function(a, e) {
                return a.title > e.title;
            }), o;
        }
        return {};
    },
    onPreviewSlides: function(a) {
        var e = a.currentTarget.dataset.url, t = a.currentTarget.dataset.category, o = a.currentTarget.dataset.package, n = [];
        this.data.categories.forEach(function(a) {
            a.id === t && (a.hasSubItems ? a.packages.forEach(function(a) {
                a.id === o && a.gallery.forEach(function(a) {
                    n.push(a.url.raw);
                });
            }) : a.gallery.forEach(function(a) {
                n.push(a.url.raw);
            }));
        }), wx.previewImage({
            current: e,
            urls: n
        });
    },
    loadThemePhotos: function(a) {
        var e = a.currentTarget.dataset.id;
        console.log("e.currentTarget.dataset", a.currentTarget.dataset), wx.navigateTo({
            url: "/pages/themes/themes?category_id=" + e,
            success: function(a) {},
            fail: function(a) {},
            complete: function(a) {}
        });
    },
    loadMorePhotos: function(a) {
        return;
    },
    onSwitchTab: function(a) {
        var t = a.currentTarget.dataset.categoryid, o = this.calcPackageInfo(this.data.categories, t), n = e.artworkTabs(t, this.data.brandType).find(function(a) {
            return a.category_id === t;
        });
        this.setData({
            categoryId: t,
            artworkTabs: e.artworkTabs(t, this.data.brandType),
            packageInfo: o,
            packageId: o.packages[0].id,
            className: n ? n.className : "default"
        });
    },
    onSelectPackage: function(e, t, o) {
        var n = e && e.currentTarget.dataset.packageid || o, i = e && e.currentTarget.dataset.categoryid || t;
        console.info("package_id", n), console.info("category_id", i);
        var r = this.data.categories.find(function(a) {
            return a._id === i;
        });
        console.log("categories", r);
        var c = r.packages.filter(function(a) {
            return a._id === n;
        }), s = 0, l = 0;
        c.forEach(function(e) {
            console.info("check inventory_restrictions", e.inventory_restrictions), "weekend" === e.inventory_restrictions ? a.globalData.cart.inventory_restrictions = "weekend" : delete a.globalData.cart.inventory_restrictions, 
            s += e.price, l += e.advance_price;
        }), this.setData({
            packageId: n
        }), a.globalData.campaign && "bday1810" === a.globalData.campaign.id && (console.info("有优惠活动", a.globalData.campaign), 
        s *= a.globalData.campaign.discount), a.globalData.cart.items = c, a.globalData.cart.totalPrice = s, 
        a.globalData.cart.prepaidPrice = l, console.info("app.globalData.cart", a.globalData.cart);
    },
    onGotUserInfo: function(a) {
        var e = this;
        console.log(a.detail.errMsg), console.log(a.detail.userInfo), console.log(a.detail.rawData), 
        t.updateWeChatUserInfo(a.detail.userInfo).then(function(a) {
            console.info("userInfo updated", a), wx.navigateTo({
                url: "/pages/schedule/schedule?store_id=" + e.data.storeId,
                success: function(a) {},
                fail: function(a) {},
                complete: function(a) {}
            });
        });
    }
});